<?php
namespace app\index\controller;
use app\index\controller\Api as Api;
use think\Controller;
use think\Db;


class Order extends Base
{

    /**
     * 下单
     * @author lukui  2017-07-20
     * @return [type] [description]
     */
    public function addorder()
    {
        $data = input('post.');

        $adddata['uid'] = $data['uid']=$this->uid;
        $conf = $this->conf;
        //持仓限制
        $allfee = Db::name('order')->where(array('ostaus'=>0,'uid'=>$data['uid']))->sum('fee');
        $allfee = $allfee?$allfee:0;
        if($allfee+$data['order_price'] > getconf('order_max_price')){     //getconf 获取网站配置信息
            // return WPreturn('持仓最大为'.getconf('order_max_price').'！',-1);
        }
        // 处理无效订单
        $this->wuxiao_order();
        //验证是否开市

        //用户信息
        $user = Db::name('userinfo')->field('*')->where('uid',$data['uid'])->find();
        //验证用户是否被冻结
        if($user['ustatus'] != 0){
            return WPreturn('您的账户已被冻结！',-1);
        }

        //-----------------------------------------------------
        //代理商购买
        //1-3级代理商高级代理（100万 50万 5万）的手续费
        //1级 扣除 手续费的60%     二级 扣除 手续费的50%   2|3
        $sx_grade = getconf('sx_grade');
        $sx_grade_arr = explode('|',$sx_grade);

        //公司拿手续费的20%
        $jin_gongs = getconf('jin_gongs');

        ///---------------------------------------
        //1-2级代理商的分配比 60|50
        $dj_grade = getconf('grade');
        $pay_grade = explode('|',$dj_grade);

        //市场部 分配比例 10%
        $shi_chang = getconf('shi_chang');
        //$shi_chang = explode('|',$dj_grade);

        //客户生成 持盈币的个数  佣金的10%
        $kehu_c = getconf('kehu_c');

        //三级分销比例（客户）20|20|20
        $sj_fenxiao = getconf('sj_fenxiao');
        //$fenxiao = explode('|',$sj_fenxiao);


        //客户交易1-2级代理获利 手续费的10%
        $fen_grade = getconf('fen_grade');

        //客户每交易一笔返公司的手续费的20%
        $sj_kehu = getconf('sj_kehu');

        //手续费 百分之十的手续费     订单价格 * 手续费的百分比   针对普通客户来说是10%
        $web_poundage = round($data['order_price']*$conf['web_poundage']/100,2);

        //一等级代理商 月100万 手续费 2%
        //二等级代理商 月50万  手续费 3%

        //持盈盾 分配
        //101 是有代理发展的

        $chiyingdun=$web_poundage;
        $bili_config['gs']=4;
        $bili_config['daili_yiji']=0;
        $bili_config['ziji']=0;
        $bili_config['shangji']=0;

        $default_system_adminid =config('default_system_adminid');
        // 订单号
        $orderno =date('YmdHis').rand(1111,9999);
        if($user['otype'] ==101 && in_array($user['dailigrade'] ,array(1,2))) {
            //一等级 代理商 提成的 60%
            if ($user['dailigrade'] == 1){
                $bili_config['gs'] = 0.4;
                $bili_config['ziji'] = 0.6;
                $content = '一级代理持赢盾';
                //----- 处理一级代理的
                $ziji_dun = $chiyingdun * $bili_config['ziji'];
                Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                $zhanbi = $bili_config['ziji'];
                set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', $content, 0, $web_poundage, $zhanbi, $orderno, $data['order_price']);
                //----- 处理公司部分
                $gongsi_dun = $chiyingdun * $bili_config['gs'];
                Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                $zhanbi = $bili_config['gs'];
                set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾', 0, $web_poundage, $zhanbi, $orderno, $data['order_price']);

            } else {

                // 判断二级代理商 没有上级
                if (in_array($user['oid'], array(0, $user['uid'], $default_system_adminid))) {
                    $bili_config['gs'] = 0.6;
                    $bili_config['ziji'] = 0.4;
                    $content = '二级代理持赢盾';
                    //----- 处理二级代理的
                    $ziji_dun = $chiyingdun * $bili_config['ziji'];
                    Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                    $zhanbi = $bili_config['ziji'];
                    set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', $content, 0, $web_poundage, $zhanbi, $orderno, $data['order_price']);
                    //----- 处理公司部分
                    $gongsi_dun = $chiyingdun * $bili_config['gs'];
                    Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                    $zhanbi = $bili_config['gs'];
                    set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾', 0, $web_poundage, $zhanbi, $orderno, $data['order_price']);

                } else {
                    // 二级代理商是一级代理推荐
                    //if (!in_array($user['vid'], array($user['oid'],0, $user['uid'], $default_system_adminid))) {
                    //用户信息
                    $__puser_info = Db::name('userinfo')->field('*')->where('uid', $user['oid'])->find();
                    ////-----------二级代理商交易100持盈豆，二级代理商获得4个盾，一级代理商获得2个
                    ///
                    if ($__puser_info && $__puser_info['dailigrade'] == 1) {

                        //----- 处理二级代理的
                        $ziji_dun = $chiyingdun * 0.4;
                        Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', 0.4);
                        //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                        set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '二级代理持赢盾', 0, $web_poundage, 0.4, $orderno, $data['order_price']);

                        //----- 处理一级代理的
                        $ziji_dun = $chiyingdun * 0.2;
                        Db::name('userinfo')->where('uid', $user['oid'])->setInc('moneybi', 0.2);
                        //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                        set_yingdun_log($user['oid'], 1, $ziji_dun, '持赢盾', '一级代理持赢盾', 0, $web_poundage, 0.2, $orderno, $data['order_price']);

                        //----- 处理公司部分
                        $gongsi_dun = $chiyingdun * 0.4;
                        Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                        set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾', 0, $web_poundage, 0.4, $orderno, $data['order_price']);

                    }
                    // }
                }
            }
            // 代理直接交易处理完毕
        }else {

            if ($user['vid'] > 0) {
                $vuser_info = Db::name('userinfo')->where('uid', $user['vid'])->find();
                // 判断是否代理商
                if (isset($vuser_info['otype']) && $vuser_info['otype'] == 101) {

                    // 处理数据是一级代理发展的----并且发展到多级
                    if ($vuser_info['dailigrade'] == 1) {
                        // 上级不是代理---代理--用户-用户
                        if ($user['oid'] != $user['vid'] && $user['oid'] > 0) {

                            $content = '一级代理持赢盾';
                            $ziji_dun = $chiyingdun * 0.1;
                            Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                            $ziji_dun = $chiyingdun * 0.1;
                            Db::name('userinfo')->where('uid', $user['oid'])->setInc('moneybi', $ziji_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            $p_userinfo = Db::name('userinfo')->where('uid', $user['oid'])->find();
                            set_yingdun_log($user['oid'], 1, $ziji_dun, '持赢盾', '上级用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                            //----- 处理一级代理的
                            $ziji_dun = $chiyingdun * 0.4;
                            Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $ziji_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            set_yingdun_log($user['vid'], 1, $ziji_dun, '持赢盾', '一级代理持赢盾', 0, $web_poundage, 0.4, $orderno, $data['order_price']);
                            //----- 处理公司部分
                            $gongsi_dun = $chiyingdun * 0.4;
                            Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                            $zhanbi = $bili_config['gs'];
                            set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾',  0, $web_poundage, 0.4, $orderno, $data['order_price']);
                        } else {
                            // 一级代理的直接下线-----------------只有两级
                            $content = '一级代理持赢盾';

                            //----- 处理一级代理的直接下线
                            $ziji_dun = $chiyingdun * 0.1;
                            Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾',0, $web_poundage, 0.1, $orderno, $data['order_price']);

                            //----- 处理一级代理的
                            $ziji_dun = $chiyingdun * 0.5;
                            Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $ziji_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            set_yingdun_log($user['vid'], 1, $ziji_dun, '持赢盾', '一级代理持赢盾', 0, $web_poundage, 0.5, $orderno, $data['order_price']);
                            //----- 处理公司部分
                            $gongsi_dun = $chiyingdun * 0.4;
                            Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                            set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾', 0, $web_poundage, 0.4, $orderno, $data['order_price']);

                        }
                    } else {
                        // 处理用户是二级代理发展
                        // 处理二级代理没有上级（一级）
                        // $vuser_info所属的二级代理
                        if (in_array($vuser_info['oid'], array(0, $vuser_info['uid'], $default_system_adminid))) {

                            // 上级不是代理
                            if ($user['oid'] != $user['vid'] && $user['oid'] > 0) {

                                $content = '二级代理持赢盾';
                                $ziji_dun = $chiyingdun * 0.1;
                                Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                                //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                                $ziji_dun = $chiyingdun * 0.1;
                                Db::name('userinfo')->where('uid', $user['oid'])->setInc('moneybi', $ziji_dun);
                                //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                //$p_userinfo = Db::name('userinfo')->where('uid', $user['oid'])->find();
                                set_yingdun_log($user['oid'], 1, $ziji_dun, '持赢盾', '上级用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                                //----- 处理二级代理的
                                $ziji_dun = $chiyingdun * 0.2;
                                Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $ziji_dun);
                                //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                set_yingdun_log($user['vid'], 1, $ziji_dun, '持赢盾', '二级代理持赢盾', 0, $web_poundage, 0.2, $orderno, $data['order_price']);
                                //----- 处理公司部分
                                $gongsi_dun = $chiyingdun * 0.6;
                                Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                                set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾', 0, $web_poundage, 06, $orderno, $data['order_price']);

                            } else {


                                //----- 处理二级代理的直接下线
                                $ziji_dun = $chiyingdun * 0.1;
                                Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                                //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                                //----- 处理二级代理的
                                $ziji_dun = $chiyingdun * 0.3;
                                Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $ziji_dun);
                                //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                set_yingdun_log($user['vid'], 1, $ziji_dun, '持赢盾', '二级代理持赢盾',  0, $web_poundage, 0.3, $orderno, $data['order_price']);
                                //----- 处理公司部分
                                $gongsi_dun = $chiyingdun * 0.6;
                                Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                                set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾',0, $web_poundage, 0.6, $orderno, $data['order_price']);
                            }
                        } else {
                            // 处理二级代理上级为一级代理
                            $root_vuser_info = Db::name('userinfo')->where('uid', $vuser_info['oid'])->find();
                            if (isset($root_vuser_info['otype']) && $root_vuser_info['otype'] == 101 && $root_vuser_info['dailigrade'] == 1) {
                                // 上级不是代理
                                /* if ($user['oid']==$root_vuser_info['uid'] && $user['vid']== 0) {

                                     //----- 处理二级代理的
                                     $ziji_dun = $chiyingdun * 0.4;
                                     Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                                     //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                     set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '二级代理持赢盾', 0, $web_poundage, 0.4, $orderno, $data['order_price']);

                                     //----- 处理一级代理的
                                     $ziji_dun = $chiyingdun * 0.2;
                                     Db::name('userinfo')->where('uid', $root_vuser_info['uid'])->setInc('moneybi', $ziji_dun);
                                     //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                     set_yingdun_log($root_vuser_info['uid'], 1, $ziji_dun, '持赢盾', '一级代理持赢盾', 0, $web_poundage, 0.2, $orderno, $data['order_price']);

                                     //----- 处理公司部分
                                     $gongsi_dun = $chiyingdun * 0.4;
                                     Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                                     set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾',  0, $web_poundage, 0.4, $orderno, $data['order_price']);

                                 }
                                 elseif ($user['oid'] != $user['vid'] && $user['oid'] > 0) {
                                */
                                if ($user['oid'] != $user['vid'] && $user['oid'] > 0) {



                                    $ziji_dun = $chiyingdun * 0.1;
                                    Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                                    $ziji_dun = $chiyingdun * 0.1;
                                    Db::name('userinfo')->where('uid', $user['oid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    $p_userinfo = Db::name('userinfo')->where('uid', $user['oid'])->find();
                                    set_yingdun_log($user['oid'], 1, $ziji_dun, '持赢盾', '上级用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                                    //----- 处理二级代理的
                                    $ziji_dun = $chiyingdun * 0.2;
                                    Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    set_yingdun_log($user['vid'], 1, $ziji_dun, '持赢盾', '二级代理持赢盾',  0, $web_poundage, 0.2, $orderno, $data['order_price']);

                                    //----- 处理一级代理的
                                    $ziji_dun = $chiyingdun * 0.2;
                                    Db::name('userinfo')->where('uid', $root_vuser_info['uid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    set_yingdun_log($root_vuser_info['uid'], 1, $ziji_dun, '持赢盾', '一级代理持赢盾',  0, $web_poundage, 0.2, $orderno, $data['order_price']);


                                    //----- 处理公司部分
                                    $gongsi_dun = $chiyingdun * 0.4;
                                    Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                                    set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾', 0, $web_poundage, 0.4, $orderno, $data['order_price']);

                                } else {


                                    //----- 处理二级代理的直接下线
                                    $ziji_dun = $chiyingdun * 0.1;
                                    Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                                    //----- 处理二级代理的
                                    $ziji_dun = $chiyingdun * 0.3;
                                    Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    set_yingdun_log($user['vid'], 1, $ziji_dun, '持赢盾', '二级代理持赢盾', 0, $web_poundage, 0.3, $orderno, $data['order_price']);

                                    //----- 处理一级代理的
                                    $ziji_dun = $chiyingdun * 0.2;
                                    Db::name('userinfo')->where('uid', $root_vuser_info['uid'])->setInc('moneybi', $ziji_dun);
                                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                                    set_yingdun_log($root_vuser_info['uid'], 1, $ziji_dun, '持赢盾', '一级代理持赢盾', 0, $web_poundage, 0.2, $orderno, $data['order_price']);

                                    //----- 处理公司部分
                                    $gongsi_dun = $chiyingdun * 0.4;
                                    Db::name('userinfo')->where('uid', $default_system_adminid)->setInc('moneybi', $gongsi_dun);
                                    set_yingdun_log($default_system_adminid, 1, $gongsi_dun, '持赢盾', '公司持赢盾',  0, $web_poundage, 0.4, $orderno, $data['order_price']);
                                }
                            }
                        }
                    }
                }elseif(isset($vuser_info['otype']) && $vuser_info['otype'] == 102) {
                    // 处理市场部分
                    if ($user['oid'] == $user['vid'] && $user['oid'] > 0) {
                        //----- 处理市场下线
                        $ziji_dun = $chiyingdun * 0.1;
                        Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                        //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                        set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                        //----- 处理市场部分
                        $gongsi_dun = $chiyingdun * 0.9;
                        Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $gongsi_dun);
                        set_yingdun_log($user['vid'], 1, $gongsi_dun, '持赢盾', '市场持赢盾', 0, $web_poundage, 0.9, $orderno, $data['order_price']);

                    }else {
                        //----- 当下多级处理
                        $ziji_dun = $chiyingdun * 0.1;
                        Db::name('userinfo')->where('uid', $user['uid'])->setInc('moneybi', $ziji_dun);
                        //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                        set_yingdun_log($user['uid'], 1, $ziji_dun, '持赢盾', '用户持赢盾',0, $web_poundage, 0.1, $orderno, $data['order_price']);

                        //----- 多级上级
                        $ziji_dun = $chiyingdun * 0.1;
                        Db::name('userinfo')->where('uid', $user['oid'])->setInc('moneybi', $ziji_dun);
                        //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                        set_yingdun_log($user['oid'], 1, $ziji_dun, '持赢盾', '上级用户持赢盾', 0, $web_poundage, 0.1, $orderno, $data['order_price']);

                        //----- 处理市场部分
                        $gongsi_dun = $chiyingdun * 0.9;
                        Db::name('userinfo')->where('uid', $user['vid'])->setInc('moneybi', $gongsi_dun);
                        set_yingdun_log($user['vid'], 1, $gongsi_dun, '持赢盾', '市场持赢盾', 0, $web_poundage, 0.9, $orderno, $data['order_price']);

                    }

                }
            }else{
                // 没有上级

                //----- 直接注册持赢盾的

                if (in_array($user['oid'], array(0, $default_system_adminid)) && $user['vid'] == 0) {
                    $ziji_dun =$chiyingdun*0.1;
                    Db::name('userinfo')->where('uid',$user['uid'])->setInc('moneybi',$ziji_dun);
                    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                    set_yingdun_log($user['uid'],1,$ziji_dun,'持赢盾','直接注册持赢盾',0,$web_poundage,0.6,$orderno,$data['order_price']);
                    //----- 处理公司部分
                    $gongsi_dun =$chiyingdun*0.9;
                    Db::name('userinfo')->where('uid',$default_system_adminid)->setInc('moneybi',$gongsi_dun);
                    set_yingdun_log($default_system_adminid,1,$gongsi_dun,'持赢盾','公司持赢盾',0,$web_poundage,0.4,$orderno,$data['order_price']);


                }else{
                    if (!in_array($user['oid'], array(0, $default_system_adminid)) && $user['vid'] == 0) {
                        $root_ouser_info = Db::name('userinfo')->where('uid', $user['oid'])->find();
                        if($root_ouser_info && $root_ouser_info['oid'] ==$default_system_adminid){
                            $ziji_dun =$chiyingdun*0.1;
                            Db::name('userinfo')->where('uid',$user['uid'])->setInc('moneybi',$ziji_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            set_yingdun_log($user['uid'],1,$ziji_dun,'持赢盾','直接注册持赢盾',0,$web_poundage,0.6,$orderno,$data['order_price']);

                            $o_dun =$chiyingdun*0.1;
                            Db::name('userinfo')->where('uid',$user['oid'])->setInc('moneybi',$o_dun);
                            //用户id  类型  变动金额 标题 说明  订单id  此刻余额
                            set_yingdun_log($user['oid'],1,$o_dun,'持赢盾','直接注册持上线赢盾',0,$web_poundage,0.6,$orderno,$data['order_price']);


                            //----- 处理公司部分
                            $gongsi_dun =$chiyingdun*0.8;
                            Db::name('userinfo')->where('uid',$default_system_adminid)->setInc('moneybi',$gongsi_dun);
                            set_yingdun_log($default_system_adminid,1,$gongsi_dun,'持赢盾','公司持赢盾',0,$web_poundage,0.4,$orderno,$data['order_price']);


                        }

                    }
                }
            }
        }

        /*
	//三级分销制度
	//---------------------------------------------------------------------------------
	//判断自己
		if($user['otype'] ==101){

			  //一等级 代理商 提成的 60% 
			if($user['dailigrade'] ==1){
										//一级代理商生成 60% 持赢币
				$web_yi = round($web_poundage*$pay_grade[0]/100,2);	
				
				//一级代理商本身产生六个持盈币
				//二级代理商本身产生五个持盈币
				//检查是不是属于市场部还是属于公司  1市场部  0 属于公司
				if($user['shichang'] ==1)
				{
					//$dai_chi = round($web_yi*10/100,2);	
					//代理商本身产生持盈币
					Db::name('userinfo')->where('uid',$user['uid'])->setInc('moneybi',$web_yi);
					           //用户id  类型  变动金额 标题 说明  订单id  此刻余额
				    set_price_log($user['uid'],1,$web_yi,'持赢币','一级代理持赢币',$oid=0,$web_poundage);

					$shi_ch = round($web_poundage*$shi_chang/100,2);
					//市场部提成  加到持赢币中
				    Db::name('userinfo')->where('uid',$user['cid'])->setInc('moneybi',$shi_ch);

				    //总佣金减去 代理商本身的产生的 减去 市场部提成
				    $gsi = round($web_poundage-$web_yi-$shi_ch,2);

				    //公司提成  加到持赢币中
				    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$gsi);

				}else
				{
					//代理商属于公司的
					//一级代理产生 60% 持赢币
					Db::name('userinfo')->where('uid',$user['uid'])->setInc('moneybi',$web_yi);

				    //公司提成  加到持赢币中
				    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_poundage-$web_yi);

				    //用户id  类型  变动金额 标题 说明  订单id  此刻余额
				     set_price_log(2272,1,$web_poundage,'持赢币','公司返回持赢币',$oid=0,$web_poundage);
				}
				            
				//二级代理商提成的 30%  
			}elseif($user['dailigrade'] ==2)
			{
				//二级代理商提成  50%
				$web_er = round($web_poundage*$pay_grade[1]/100,2);	

				//二级代理商本身产生  50%
			   Db::name('userinfo')->where('uid',$user['uid'])->setInc('moneybi',$web_er);
                
				//检查是不是属于市场部还是属于公司  1市场部  0属于公司
				
			    //查出二级代理商上级是什么  一级代理商 或者 市场部
				$user_shi = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,shichang,cid')->where('uid',$user['oid'])->find();

				if($user['shichang'] ==1)
				{			
					if($user_shi['otype'] == 101) //上级是一级代理商
					{
						$dai_chier = round($web_poundage*10/100,2);	
						//一级代理商  添加持盈币
					    Db::name('userinfo')->where('uid',$user_shi['uid'])->setInc('moneybi',$dai_chier);

					    $shi_ch = round($web_poundage*$shi_chang/100,2);
						//市场部提成  加到持赢币中
					    Db::name('userinfo')->where('uid',$user_shi['cid'])->setInc('moneybi',$shi_ch);

					      $gssi = round($web_poundage-$web_er-$shi_ch-$dai_chier,2);
					     //公司提成  加到持赢币中
				    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$gssi);
					}else                 //上级是市场部
					{						
						  $shi_ch = round($web_poundage*$shi_chang/100,2);

						//市场部提成  加到持赢币中
					    Db::name('userinfo')->where('uid',$user_shi['uid'])->setInc('moneybi',$shi_ch);
					    $gsii = round($web_poundage-$web_er-$shi_ch,2);

					     //公司提成  加到持赢币中
				    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$gsii);
					}

				}else  //不属于市场部 属于公司
				{
					if($user_shi['otype'] == 101) //上级是一级代理商
					{
						$dai_chier = round($web_poundage*10/100,2);	
						//一级代理商  添加持盈币
					    Db::name('userinfo')->where('uid',$user_shi['uid'])->setInc('moneybi',$dai_chier);

					    $gssi = round($web_poundage-$web_er-$dai_chier,2);
					     //公司提成  加到持赢币中
				    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$gssi);			
					}else     //属于公司
					{
					    $gsii = round($web_poundage-$web_er,2);

					     //公司提成  加到持赢币中
				    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$gsii);			    	 
					}
				}
			}

		}else{  //用户是客户		
			//手续费 百分之十的手续费    订单价格 * 手续费的百分比   针对普通客户来说是10%		
			$web_pou = round($data['order_price']*$conf['web_poundage']/100,2);	
			///判断用户是 市场部 还是 公司的     1 市场部 0 公司
			   //查出二级代理商上级是什么  一级代理商 或者 市场部
		       $user_shi = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,shichang,cid')->where('uid',$user['oid'])->find();

				$ke_c = round($web_pou*$kehu_c/100,2);	
				//客户本身产生持盈币
				Db::name('userinfo')->where('uid',$user['uid'])->setInc('moneybi',$ke_c);	

				if($user['shichang'] == 1){	 					
					$user2 = Db::name('userinfo')->where('uid',$user['oid'])->find();

					//判断他的上级是   查出上一级的客户
				   if($user2['otype'] == 102){	        //上级用户是 市场部的
						//$web_pou 本金的10% 的佣金 
						  $shi_ch = round($web_pou*$shi_chang/100,2);
						//市场部提成  加到持赢币中
					    Db::name('userinfo')->where('uid',$user_shi['cid'])->setInc('moneybi',$shi_ch);

					    //公司提成  加到持赢币中				 
				    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$shi_ch);
				
					}elseif($user2['otype'] == 101 && $user2['dailigrade'] == 1)//上级 一级代理
					{		
    				    $web_r = round($web_pou*$pay_grade[0]/100,2);

						//一级代理商加					
						Db::name('userinfo')->where('uid',$user['oid'])->setInc('moneybi',$web_r);

						set_price_log($user['oid'],1,$web_poundage*$pay_grade[0]/100,'佣金','一级代理返回佣金',$oid=0,$web_poundage);

						$shi_ch = round($web_pou*$shi_chang/100,2);
                        
						//市场部提成  加到持赢币中
					    Db::name('userinfo')->where('uid',$user['cid'])->setInc('moneybi',$shi_ch);
						//公司提成                           //总佣金-客户-市场-一级
						Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$shi_ch-$web_r);

						set_price_log(2272,1,$web_poundage*$jin_gongs/100,'佣金','公司返回佣金',$oid=0,$web_poundage);

					}elseif($user2['otype'] ==101 && $user2['dailigrade'] ==2) 
					//上级如果是二级代理商
					{						
						$dai_c = round($web_pou*$pay_grade[1]/100,2);	
						//二级代理商 添加持盈币
				       Db::name('userinfo')->where('uid',$user2['uid'])->setInc('moneybi',$dai_c);

					    //查出二级代理商上级是什么  一级代理商 或者 市场部
					      $user_h = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,shichang,cid')->where('uid',$user2['oid'])->find();

						if($user_h['otype'] == 101) //上上级是一级代理商
					   {
							$dai_chier = round($web_pou*10/100,2);	
							//一级代理商  添加持盈币
						    Db::name('userinfo')->where('uid',$user_shi['uid'])->setInc('moneybi',$dai_chier);

						    $shi_ch = round($web_pou*$shi_chang/100,2);
							//市场部提成  加到持赢币中
						    Db::name('userinfo')->where('uid',$user_shi['cid'])->setInc('moneybi',$shi_ch);

						     //公司提成  加到持赢币中
					    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$dai_chier-$shi_ch-$dai_c-$ke_c);

					   }else                 //上上级是市场部
					   {
							$shi_ch = round($web_pou*$shi_chang/100,2);
							//市场部提成  加到持赢币中
						    Db::name('userinfo')->where('uid',$user_shi['cid'])->setInc('moneybi',$shi_ch);

						     //公司提成  加到持赢币中
					    	Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$dai_c-$shi_ch-$ke_c);
					   }					   
				
					}elseif($user2['otype'] == 0)  //上级是客户
					{
						$ke_c = round($web_pou*$kehu_c/100,2);
						//如果上级是客户 提成的10%	
						Db::name('userinfo')->where('uid',$user2['uid'])->setInc('moneybi',$ke_c);

						set_price_log($user['oid'],1,$web_poundage*$sj_fenxiao/100,'佣金','客户返回佣金',$oid=0,$web_poundage);
						
						//查出上上级
						$user3 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,vid,shichang,cid')->where('uid',$user2['oid'])->find();

						//上上级如果是一级代理商 
						if($user3['otype'] == 101 && $user3['dailigrade'] ==1)
						{					
                             $yiji_ch = round($web_pou*($pay_grade[0]-20)/100);
							//一级代理商加					
							 Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$yiji_ch);
							
							 set_price_log($user2['oid'],1,$web_poundage*($pay_grade[0]-$fenxiao)/100,'佣金','一级代理返回佣金',$oid=0,$web_pounda);

							  $shi_ch = round($web_pou*$shi_chang/100,2);
							 //市场部提成  加到持赢币中
						      Db::name('userinfo')->where('uid',$user3['cid'])->setInc('moneybi',$shi_ch);

							  //公司提成
							  Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$dai_c-$shi_ch-($ke_c*2)-$yiji_ch);

							   set_price_log(2272,1,$web_poundage*$jin_gongs/100,'佣金','公司返回佣金',$oid=0,$web_poundage);
						
						 //上上级如果是二级代理商 
						 }elseif($user3['otype'] ==101 && $user3['dailigrade'] ==2)
						{

					      $err_ch = round($web_pou*($pay_grade[0]-20)/100);

								//二级代理商 加					
					        Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$err_ch);

								//查出上上级
						   $user8976 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,shichang,cid')->where('uid',$user3['oid'])->find();
							////查出上上上级是什么
							if($user8976['otype'] ==101 && $user8976['dailigrade'] ==1)
							{
								 $yi_ch = round($web_pou*10/100);
									//一级代理商 加					
								 Db::name('userinfo')->where('uid',$user3['oid'])->setInc('moneybi',$yi_ch);                     
                                   
                                  $shi_ch = round($web_pou*$shi_chang/100,2);
									//市场部提成  加到持赢币中
							     Db::name('userinfo')->where('uid',$user3['cid'])->setInc('moneybi',$shi_ch);

								 //公司提成
								 Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$yi_ch-$shi_ch-$ke_c-$ke_c-$err_ch);
							}else 
							{
								$shi_ch = round($web_pou*$shi_chang/100,2);
									//市场部提成  加到持赢币中
							    Db::name('userinfo')->where('uid',$user3['cid'])->setInc('moneybi',$shi_ch);

								//公司提成
								Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$shi_ch-$ke_c-$ke_c-$err_ch);
							}	
///////////-------------------------------------------------------			
						}else//上上级是客户  查出他的代理商 
						{
                           $us564 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,shichang,cid')->where('uid',$user3['vid'])->find();

                           if($us564['dailigrade'] == 1 || $us564['otype'] == 101){

                           	    $yiji_ch = round($web_pou*($pay_grade[0])/100);
                           	    
							    //一级代理商加					
							    Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$yiji_ch);
                                
                                $shi_ch = round($web_pou*$shi_chang/100,2);
									//市场部提成  加到持赢币中
							    Db::name('userinfo')->where('uid',$us564['cid'])->setInc('moneybi',$shi_ch);

								//公司提成
								Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$shi_ch-$ke_c-$yiji_ch);

                           }elseif($us564['dailigrade'] == 2 || $us564['otype'] == 101)
                           {
                           	   $us589 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade,shichang,cid')->where('uid',$us564['oid'])->find();
                               
                                 $err_ch = round($web_pou*($pay_grade[1]-20)/100);
								//二级代理商 加					
					             Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$err_ch);

                           	   if($us589['otype'] == 101) ///如果不是 一级代理商 就是市场部门的 
                           	   {
                                    $yiji_ch = round($web_pou*($pay_grade[0])/100);
							       //一级代理商加					
							        Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$yiji_ch);
                                
                                    $shi_ch = round($web_pou*$shi_chang/100,2);
									//市场部提成  加到持赢币中
							        Db::name('userinfo')->where('uid',$us564['cid'])->setInc('moneybi',$shi_ch);

								     //公司提成
								    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$shi_ch-$ke_c-$yiji_ch);
                                
                           	   }else
                           	   {
                                      $shi_ch = round($web_pou*$shi_chang/100,2);
									//市场部提成  加到持赢币中
							        Db::name('userinfo')->where('uid',$us564['cid'])->setInc('moneybi',$shi_ch);

								    //公司提成
								    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$shi_ch-$ke_c-$ke_c-$err_ch);
                           	   }
                           }else         //上上 上级 是市场部门
                           {
                                    $shi_ch = round($web_pou*$shi_chang/100,2);
									//市场部提成  加到持赢币中
							        Db::name('userinfo')->where('uid',$us564['cid'])->setInc('moneybi',$shi_ch);

								    //公司提成
								    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$shi_ch-$ke_c-$ke_c);                             
                           }
						}
					}
   
				}else   ///如果用户不是 市场部门的人
				{	                   
					$user2 = Db::name('userinfo')->where('uid',$user['oid'])->find();
					//上级如果是代理商  一级
					//手续费 百分之十的手续费 订单价格 * 手续费的百分比  针对普通客户来说是10%	
					$web_pou = round($data['order_price']*$conf['web_poundage']/100,2);	
					
					//客户属于
					if($user['oid'] == 2272){
						//公司下的客户直接返现给公司 90%手续费			
					    Db::name('userinfo')->where('uid',$user['oid'])->setInc('moneybi',$web_poundage-$ke_c);

					  set_price_log(2272,1,$web_poundage,'佣金','公司返回佣金',$oid=0,$web_poundage);
				  
					//判断他的上级是   查出上一级的客户
					}elseif($user2['otype'] ==101 && $user2['dailigrade'] ==1)
					{				
                        $ert = round($web_poundage*($pay_grade[0]-10)/100,2);
						//一级代理商加					
					    Db::name('userinfo')->where('uid',$user['oid'])->setInc('moneybi',$ert);

						set_price_log($user['oid'],1,$web_poundage*$pay_grade[0]/100,'佣金','一级代理返回佣金',$oid=0,$web_pounda);
						
						//公司提成
						Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_poundage-$ert-$ke_c);

						set_price_log(2272,1,$web_poundage*$jin_gongs/100,'佣金','公司返回佣金',$oid=0,$web_poundage);

					}elseif($user2['otype'] ==101 && $user2['dailigrade'] ==2) //上级如果是代理商 二级
					{
						$user4433 = Db::name('userinfo')->where('uid',$user2['oid'])->find();
                         
                         $er_jk = round($web_poundage*$pay_grade[1]/100,2);
						//二级代理商加					
						Db::name('userinfo')->where('uid',$user['oid'])->setInc('moneybi',$er_jk);

						set_price_log($user['oid'],1,$web_poundage*$pay_grade[1]/100,'佣金','二级代理返回佣金',$oid=0,$web_pounda);

                         $er_yk = round($web_poundage*10/100,2);

						//一级代理商加					
						Db::name('userinfo')->where('uid',$user4433['uid'])->setInc('moneybi',$er_yk);

						set_price_log($user4433['uid'],1,$web_poundage*10/100,'佣金','一级代理返回佣金',$oid=0,$web_pounda);

						//公司提成
						Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_poundage-$er_jk-$er_yk);

						set_price_log(2272,1,$web_poundage*$jin_gongs/100,'佣金','公司返回佣金',$oid=0,$web_poundage);
						
					}elseif($user2['otype'] == 0)  //上级是客户
					{

						$ke_c = round($web_pou*$kehu_c/100,2);
						//如果上级是客户					
						Db::name('userinfo')->where('uid',$user['oid'])->setInc('moneybi',$ke_c);

						set_price_log($user['oid'],1,$web_poundage*$sj_fenxiao/100,'佣金','客户返回佣金',$oid=0,$web_poundage);
						
						//查出上上级
						$user3 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade')->where('uid',$user2['oid'])->find();
                           
						//上上级如果是一级代理商 
						 if($user3['otype'] ==101 && $user3['dailigrade'] ==1)
						 {			
                            $yiji_ch = round($web_pou*($pay_grade[0]-20)/100);
							//一级代理商加					
							 Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$yiji_ch);
							
							//公司提成
							Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_poundage-$yiji_ch-$ke_c);

							set_price_log(2272,1,$web_poundage*$jin_gongs/100,'佣金','公司返回佣金',$oid=0,$web_poundage);
						
						    //上上级如果是二级代理商 
						 }elseif($user3['otype'] ==101 && $user3['dailigrade'] ==2)
						 {
						 	
                            $tr_ch = round($web_poundage*10/100);
							//一级代理商 加					
							Db::name('userinfo')->where('uid',$user3['oid'])->setInc('moneybi',$tr_ch);

							set_price_log($user3['oid'],1,$web_poundage*($pay_grade[0]-$pay_grade[1])/100,'佣金','一级代理返回佣金',$oid=0,$web_pounda);

                              $err_ch = round($web_pou*($pay_grade[0]-20)/100);

							//二级代理商 加					
							Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$err_ch);

							set_price_log($user2['oid'],1,$web_poundage*($pay_grade[1]-$fenxiao)/100,'佣金','一级代理返回佣金',$oid=0,$web_pounda);

							//公司提成
							Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_poundage-$err_ch-$tr_ch-($ke_c*2));

							set_price_log(2272,1,$web_poundage*$jin_gongs/100,'佣金','公司返回佣金',$oid=0,$web_poundage);
						    //上上级如果是客户     查出他的代理商
						 }elseif($user3['otype'] ==0)
						 {
                             
						////----------------------------------------------------	            
						     $us564 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade')->where('uid',$user3['oid'])->find();

                           if($us564['dailigrade'] == 1 || $us564['otype'] == 101){

                           	     $yiji_ch = round($web_pou*($pay_grade[0])/100);
							    //一级代理商加					
							    Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$yiji_ch);
                                
								//公司提成
								Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$yiji_ch);

                           }elseif($us564['dailigrade'] == 2 || $us564['otype'] == 101)
                           //二级代理商
                           {
                           	   $us589 = Db::name('userinfo')->field('ustatus,usermoney,uid,otype,oid,dailigrade')->where('uid',$us564['oid'])->find();
                               
                                 $err_ch = round($web_pou*($pay_grade[1]-20)/100);
								//二级代理商 加					
					             Db::name('userinfo')->where('uid',$user2['oid'])->setInc('moneybi',$err_ch);

                           	   if($us589['otype'] == 101) ///如果不是 一级代理商 
                           	   {
                                    $yiji_ch = round($web_pou*($pay_grade[0])/100);

							        //一级代理商加					
                                    Db::name('userinfo')->where('uid',$us589['uid'])->setInc('moneybi',$yiji_ch);
								     //公司提成
								    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$yiji_ch);
                                
                           	   }else
                           	   {         
           						    //公司提成
								    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$ke_c);
                           	   }
                           }else    //客户属于公司
                           {        
                           
                                 //公司提成
								    Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$ke_c);
                           }

						////------------------------------------------------------	
						}else
						{
							//公司提成
						  Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$ke_c);
						}

					}elseif($user2['otype'] == 3) 
					{				
                         //公司提成
						Db::name('userinfo')->where('uid',2272)->setInc('moneybi',$web_pou-$ke_c-$ke_c);

					}
			}
		}
        */

//------------------------------------------------------------------------------------------

        //验证余额是否够
        if($user['usermoney'] < $data['order_price']){
            return WPreturn('您得余额不足，请充值！',-1);
        }
        //验证金额 10 ~ 3000
        if($data['order_price'] < $conf['order_min_price'] || $data['order_price'] > $conf['order_max_price']){
            return WPreturn('抱歉！单笔持仓在'.$conf['order_min_price'].'~'.$conf['order_max_price'].'之间！',-1);
        }
        //获取手续费的百分比
        $web_poundage2 = getconf('web_poundage');

        //增加用户建仓时价格   滑点
        $sj_hua = getconf('sj_huadian');
        $sj_huadian = explode('|',$sj_hua);

        //建仓
        $adddata['buytime'] = time();    //当前时间
        $adddata['endprofit'] = $data['order_sen'];   //点数分钟数
        $adddata['pid'] = $data['order_pid'];         // 产品id
        $adddata['ostyle'] = $data['order_type'];     //0涨 1 跌  涨幅状态
        $adddata['buyprice'] = $data['newprice']+$sj_huadian[0];     //入仓价  加上滑点

        $adddata['endloss'] = $data['order_shouyi'];  //盈亏比例
        $adddata['fee'] = $data['order_price'];        //总费用 建仓用的金额
        $adddata['sx_price'] = $data['order_price']-$web_poundage;   //扣除手续费的金额

        $adddata['eid'] = 2;                          //1点位 2时间
        $adddata['selltime']=$adddata['buytime']+$adddata['endprofit'];   //平仓

        $adddata['ptitle'] = db('productinfo')->where('pid',$data['order_pid'])->value('ptitle');   //商品名称
        $adddata['ostaus']='0';
        if(!isset($data['beishu'])){
            $data['beishu'] =1.6;
        }
        if(!isset($data['goods_price'])){
            $data['goods_price'] =$data['order_price'];
        }

        $adddata['beishu']=$data['beishu'];
        $adddata['goods_price']=$data['goods_price'];


        if($user['otype'] ==101){

            //一等级 代理商 提成的 2%
            if($user['dailigrade'] ==1){

                $adddata['sx_fee']=round($data['order_price']*$sx_grade_arr[0]/100 ,2); //手续费


            }elseif($user['dailigrade'] ==2)
            {
                //$web_pou33 = round($data['order_price']*$sx_grade_arr[1]/100,2);

                $adddata['sx_fee']=round($data['order_price']*$sx_grade_arr[1]/100 ,2); //手续费

            }

        }else{

            $adddata['sx_fee']=round($data['order_price']*$web_poundage2/100 ,2); //手续费
        }


        //会员建仓后金额
        $adddata['commission'] = $user['usermoney'] - $adddata['fee'];  //$user['usermoney'] 账户余额

        //订单号
        $adddata['orderno']=$orderno;

        //下单
        $ids = Db::name('order')->insertGetId($adddata);
        if($ids){
            //下单成功减用户余额
            //$u_fee = $data['order_price'] + $adddata['sx_fee'];

            $u_fee = $data['order_price'];
            $editmoney = Db::name('userinfo')->where('uid',$data['uid'])->setDec('usermoney',$u_fee);

            //$nowmoney = $user['usermoney'] - ($data['order_price'] + $web_poundage);

            $nowmoney = $user['usermoney'] - $data['order_price'];
            if($nowmoney < 0) $nowmoney=0;
            set_price_log($data['uid'],2,$u_fee,'下单','下单成功',$ids,$nowmoney);

            //------------------------------------------------------------------------------------
            //     $msg =$this->closeTrade($uid,$user['otype'],$user['dailigrade'],$web_poundage,$user['oid'],$user['balance'],$youjia,$oid,$bdyy,$jiancj,$ykzj,$uprice,,2);
            //             // 用户id,是否代理商,代理商等级,手续费,上线的用户ID,账户余额,该产品最新价格,订单id,建仓金额,盈亏金额,产品单价, 类型
            // $this->ajaxReturn($msg);
            //-------------------------------------------------------------------------------------

            if($editmoney){
                $adddata['oid'] = $ids;

                //缓存加载验证
                $order_rand = rand(1,1000);  //返回1到1000 的随机整数
                cache('goorder_'.$ids,$order_rand,$adddata['endprofit']+10);
                $adddata['order_rand'] = $order_rand;

                return WPreturn($adddata,1);
            }else{
                Db::table('order')->where('oid',$ids)->delete();
                return WPreturn('下单失败，请重试！',-1);
            }

        }else{
            return WPreturn('下单失败，请重试！',-1);
        }
        //dump($data);
    }

    /**
     * ajax 通过产品id 获取用户订单，
     * @author lukui  2017-07-22
     * @return [type] [description]
     */
    public function ajaxorder()
    {
        $uid = $_SESSION['uid'];
        $pid = input('param.pid');
        if (empty($uid) || empty($pid)) {
            return false;
        }
        //持仓信息
        $map = array('uid'=>$uid,'ostaus'=>0,'pid'=>$pid);
        $map['selltime'] = array('gt',time());
        $hold = Db::name('order')->where($map)->order('oid desc')->select();
        if($hold){
            $hold[0]['time'] = time();
        }
        if($hold){
            return $hold;
        }else{
            return false;
        }
    }

    /**
     * ajax 获取用户未平仓订单，
     * @author lukui  2017-07-22
     * @return [type] [description]
     */
    public function ajaxorder_list()
    {
        $uid = $this->uid;
        if (empty($uid)) {
            return false;
        }
        //持仓信息
        $map = array('uid'=>$uid,'ostaus'=>0);
        $map['selltime'] = array('gt',time());

        $hold = Db::name('order')->where($map)->order('oid desc')->select();
        if($hold){
            $hold[0]['time'] = time();
        }
        if($hold){
            return $hold;
        }else{
            return false;
        }
    }

    //获取现在的价格
    public function get_price()
    {
        //此刻产品价格
        $p_map['isdelete'] = 0;
        $pro = db('productdata')->field('pid,Price')->where($p_map)->select();
        $prodata = array();
        foreach ($pro as $k => $v) {
            $prodata[$v['pid']] = $v['Price'];
        }
        return $prodata;

    }


    //获取开盘价格
    public function get_open()
    {

        //此刻产品价格
        $p_map['isdelete'] = 0;
        $pro = db('productdata')->field('pid,Open')->where($p_map)->select();
        $prodata = array();
        foreach ($pro as $k => $v) {
            $prodata[$v['pid']] = $v['Open'];
        }
        return $prodata;
    }
    /**
     * ajax 通过产品id 平仓后弹框提示，
     * @author lukui  2017-07-22
     * @return [type] [description]
     */
    public function ajaxalert()
    {
        $uid = $_SESSION['uid'];
        $pid = input('param.pid');
        if (empty($uid) || empty($pid)) {
            return false;
        }
        //持仓信息
        $hold = Db::name('order')->field('oid,ploss,fee,eid')->where(array('uid'=>$uid,'ostaus'=>1,'pid'=>$pid,'isshow'=>0))->order('oid desc')->find();
        //修改持仓信息

        $isedit = Db::name('order')->where('oid',$hold['oid'])->setField('isshow','1');
        if($hold && $isedit){
            return $hold;
        }else{
            return false;
        }
    }

    /**
     * 持仓列表
     * @author lukui  2017-07-18
     * @return [type] [description]
     */
    public function hold()
    {
        $uid = $_SESSION['uid'];
        $hold = Db::name('order')->field('oid,ptitle,buytime,fee,ostyle')->where(array('uid'=>$uid,'ostaus'=>0))->order('oid desc')->select();
        $this->assign('hold',$hold);
        return $this->fetch();
    }

    //持仓列表信息
    public function holdinfo()
    {
        $uid = $_SESSION['uid'];
        $oid = input('param.oid');
        if(!$oid){
            $this->redirect('hold');
        }
        $order = Db::name('order')->where('oid',$oid)->find();
        $this->assign($order);
        return $this->fetch();


    }


    /**
     * 订单列表
     * @author lukui  2017-07-18
     * @return [type] [description]
     */
    public function orderlist()
    {
        $uid = $this->uid;
        $hold = Db::name('order')->where(array('uid'=>$uid,'ostaus'=>1))->order('oid desc')->paginate(20);

        return $hold;

    }

    /**
     * 已平仓订单详情
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function orderinfo()
    {
        $uid = $_SESSION['uid'];
        $oid = input('param.oid');
        if(!$oid){
            $this->redirect('orderlist');
        }
        $order = Db::name('order')->where('oid',$oid)->find();
        $this->assign($order);
        return $this->fetch();

    }



    /**
     * 实时获取以平仓订单
     * @return [type] [description]
     */
    public function get_this_order()
    {

        $oid = input('param.oid');
        $map['oid'] = $oid;
        $map['ostaus'] = 1;
        $order = db('order')->where($map)->find();

        return $order;

    }

    /**
     * 实时获取以平仓订单
     * @return [type] [description]
     */
    public function get_hold_order()
    {

        $oid = input('param.oid');
        $map['oid'] = $oid;
        $map['ostaus'] = 1;
        $order = db('order')->where($map)->find();

        return $order;

    }

    /**
     * 专门处理无效订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function wuxiao_order(){
        $db_order = db('order');
        $db_userinfo = db('userinfo');
        //订单列表
        //$map['oid'] = array('oi',$oid);
        //$map['ostaus'] = 0; //0交易中  1 平仓
        //$map['selltime'] = array('elt',$nowtime);
        //$_orderlist = $db_order->where($map)->limit(0,50)->select();
        $time =time()+5;
        $_orderlist = $db_order->where('ostaus',0)->where('selltime','<=',$time)->select();
        if($_orderlist){
            foreach ($_orderlist as $key=>$v){

                //此刻可平仓价位
                $sellprice = 0;

                //增加用户建仓时价格   滑点
                $sj_hua = getconf('sj_huadian');
                $sj_huadian = explode('|',$sj_hua);

                $sellprice = $sellprice-$sj_huadian[1];

                $fee = $v['fee'];     //总费用

                $yingli = 0;
                $d_map['is_win'] = 3;
                //平仓处理订单
                $d_map['ostaus'] = 1;
                $d_map['sellprice'] = $sellprice;
                $d_map['ploss'] = $yingli;
                $d_map['oid'] = $v['oid'];
                db('order')->update($d_map);

                //平仓增加用户金额
                $u_add = $fee;
                $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);
                //写入日志
                $this->set_order_log($v,$u_add);

                //----- 处理无效盾部分
                $yingbi_list = Db::name('yingdun_log')->where('orderno',$v['orderno'])->select();
                if($yingbi_list){
                    foreach ($yingbi_list as $yb_key=>$yb_val){
                        Db::name('userinfo')->where('uid', $yb_val['uid'])->setDec('moneybi', $yb_val['account']);
                        set_yingdun_log($yb_val['uid'], 2, -$yb_val['account'], $yb_val['title'], $yb_val['content'].'无效处理',$yb_val['oid'],$yb_val['summoneybi'], $yb_val['zhanbi'], $v['orderno'], $yb_val['order_price']);
                    }
                }

            }
        }
    }


    //平仓
    public function goorder()
    {
        //$api= new Api();
        //$api->order22($oid,$order33);
        //$api->order();
        $pid = input('pid');
        $oid = input('oid');    //70742
        $price = input('price');	//0.97618 当前价格
        $order_rand = input('order_rand');//183
        $static = 1; //1成功返回并继续运行  0失败返回不运行  2 失败返回继续轮询
        // $_data23['ostaus'] = $static;
        // db('order')->where('oid',$oid)->update($_data23);  //更新表 平仓

        //平仓
        //------------------------------------------------------------
        //   $order34 = db('order')->where('oid',$oid)->find();
        // $order33 =$order34['pid'];
        //  dump($oid);
        //-------------------------------------------------------------

        //-平仓订单--------------------------------------------------------------------

        if(!isset($oid)){
            continue;
        }

        $nowtime = time();
        $s_rand = rand(6,12);
        $db_order = db('order');
        $db_userinfo = db('userinfo');
        //订单列表
        //$map['oid'] = array('oi',$oid);
        //$map['ostaus'] = 0; //0交易中  1 平仓
        //$map['selltime'] = array('elt',$nowtime);
        //$_orderlist = $db_order->where($map)->limit(0,50)->select();
        $_orderlist = $db_order->where('oid',$oid)->select();

        $data_info = db('productinfo');

        //风控参数
        $risk = db('risk')->find();

        //此刻产品价格
        //$p_map['isdelete'] = 0;
        $pro= db('productdata')->field('pid,Price')->where('pid',$pid)->find();

        // $_pro = cache('nowdata');
        // if(!isset($_pro['pid'])){

        // 	continue;
        // }
        // foreach ($pro as $k => $v) {
        // 	$pro = cache('nowdata');

        // 	if(!isset($pro[$v['pid']])){

        // 		continue;
        // 	}

        // $prodata[$v['pid']] = $this->order_type22($_orderlist,$_pro[$v['pid']],$risk,$data_info);

        // }

        //订单列表
        //$map['ostaus'] = 0;
        $map['selltime'] = array('elt',$nowtime);
        //$orderlist = $db_order->where($map)->limit(0,50)->select();

        if(!$_orderlist){
            return false;
        }

        //循环处理订单
        $nowtime = time();
        foreach ($_orderlist as $k => $v) {
            //此刻可平仓价位
            $sellprice = isset($pro['Price'])?$pro['Price']:0;

            //增加用户建仓时价格   滑点
            $sj_hua = getconf('sj_huadian');
            $sj_huadian = explode('|',$sj_hua);

            $sellprice = $sellprice-$sj_huadian[1];
            //买入价
            $buyprice = $v['buyprice'];//入仓价
            $fee = $v['fee'];     //总费用
            //平仓价                //入仓价
            //$order_cha = round(floatval($sellprice)-floatval($buyprice),6);

            /**
             * alexander--2018-07-19
             * 处理赔率为题
             */
            $_order_cha = round(floatval($sellprice)-floatval($buyprice),6);
            $xian_lv_num =0;
            $order_cha =0;
            //赔率处理--alexander--2018-07-19
            $peilv_str = getconf('peilvconfig');
            $peilv_str_1 = explode("|", $peilv_str);
            $peilv_arr =array();
            $__beishu =1;
            if(is_array($peilv_str_1)){
                foreach ($peilv_str_1 as $pvkey=>$pvval){
                    $peilv_str_2 = explode("-", $pvval);
                    $peilv_arr[$peilv_str_2[0]]['beishu'] =$peilv_str_2[0];
                    //$peilv_arr[$peilv_str_2[0]]['jiexian'] =$peilv_str_2[1];

                    $peilv_arr[$peilv_str_2[0]]['jiexian'] =$peilv_str_2[1];
                    $xian_lv_num =abs($_order_cha/$buyprice);

                }
            }
            if(is_array($peilv_arr) && count($peilv_arr)>0){
                foreach ($peilv_arr as $pvkey=>$pvval){
                    if($v['beishu'] ==$pvval['beishu']){
                        // 买涨时
                        if($v['ostyle'] == 0) {
                            //if ($xian_lv_num > $pvval['jiexian']) {
                            if ($_order_cha > $pvval['jiexian']) {
                                $order_cha = 1;

                            } else {
                                $order_cha = -1;
                            }
                        }else{
                            // 买跌时
                            //if ($_order_cha < $pvval['jiexian']) {
                            if ($xian_lv_num < $pvval['jiexian']) {
                                $order_cha = 1;

                            } else {
                                $order_cha = -1;
                            }
                        }


                        $__beishu =$v['beishu'];
                    }
                }
            }else{
                $order_cha =-1;
            }
            //-------

            //买涨
            if($v['ostyle'] == 0){// && $nowtime >= $v['selltime']


                //-----------------------------------------------------
                if($order_cha > 0){  //盈利
                    //$yingli = (($v['fee']*($__beishu)-$v['fee'])/$v['fee'])*$v['fee'];
                    $yingli = ($v['fee']*$__beishu)-$v['fee'];
                    //((5*(1.5)-5)/5)*5;
                    $d_map['is_win'] = 1;
                    //平仓增加用户金额
                    $u_add = $yingli + $fee;
                    $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);
                    //写入日志
                    $this->set_order_log($v,$u_add);

                    /*
                    if($v['endloss'] == 60 ){

                        $yingli = $v['fee']*($v['endloss']/100);
                        $d_map['is_win'] = 1;

                        //平仓增加用户金额
                           $u_add = $yingli + $fee -$v['sx_fee'];
                           $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                           //写入日志
                           $this->set_order_log($v,$u_add);

                   }else{

                           //$yingli  = $order_cha;
                        $yingli = -1*$v['fee'];
                        $d_map['is_win'] = 2;
                   }

                   if($v['endloss'] == 200 ){
                           if($order_cha >=1 && $order_cha <= 2)
                           {
                               $yingli = $v['fee']*($v['endloss']/100);
                            $d_map['is_win'] = 1;

                            //平仓增加用户金额
                               $u_add = $yingli + $fee -$v['sx_fee'];
                               $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                               //写入日志
                               $this->set_order_log($v,$u_add);

                           }else
                           {
                               //$yingli  = $order_cha;
                            $yingli = -1*$v['fee'];
                            $d_map['is_win'] = 2;
                           }
                   }

                   if ($v['endloss'] ==500) {

                           if($order_cha >2){
                               $yingli = $v['fee']*($v['endloss']/100);
                            $d_map['is_win'] = 1;

                            //平仓增加用户金额
                               $u_add = $yingli + $fee -$v['sx_fee'];
                               $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                               //写入日志
                               $this->set_order_log($v,$u_add);
                           }else{

                           //$yingli  = $order_cha;
                        $yingli = -1*$v['fee'];
                        $d_map['is_win'] = 2;
                        }
                       }*/

                }elseif($order_cha <= 0){	//亏损


                    $yingli = -1*$v['fee'];
                    $d_map['is_win'] = 2;

                }else{		//无效
                    $yingli = 0;
                    $d_map['is_win'] = 3;

                    //平仓增加用户金额
                    $u_add = $fee;
                    $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);
                    //写入日志
                    $this->set_order_log($v,$u_add);

                    //----- 处理无效盾部分
                    $yingbi_list = Db::name('yingdun_log')->where('orderno',$v['orderno'])->select();
                    if($yingbi_list){
                        foreach ($yingbi_list as $yb_key=>$yb_val){
                            Db::name('userinfo')->where('uid', $yb_val['uid'])->setDec('moneybi', $yb_val['account']);
                            set_yingdun_log($yb_val['uid'], 2, -$yb_val['account'], $yb_val['title'], $yb_val['content'].'无效处理',$yb_val['oid'],$yb_val['summoneybi'], $yb_val['zhanbi'], $v['orderno'], $yb_val['order_price']);
                        }
                    }




                }

                //-----------------------------------------------------------------------

                // if($order_cha > 0){  //盈利
                // 	//--------------------------
                // 	//--------------------------
                // 	$yingli = $v['fee']*($v['endloss']/100);
                // 	$d_map['is_win'] = 1;

                // 	//平仓增加用户金额
                //                	$u_add = $yingli + $fee;
                //                	$db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                //                	//写入日志
                //                	$this->set_order_log($v,$u_add);

                // }elseif($order_cha < 0){	//亏损
                // 	//$yingli  = $order_cha;
                // 	$yingli = -1*$v['fee'];
                // 	$d_map['is_win'] = 2;

                // }else{		//无效
                // 	$yingli = 0;
                // 	$d_map['is_win'] = 3;

                // 	//平仓增加用户金额
                //                	$u_add = $fee;
                //                	$db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);
                //                	//写入日志
                //                	$this->set_order_log($v,$u_add);
                // }

                //平仓处理订单
                $d_map['ostaus'] = 1;
                $d_map['sellprice'] = $sellprice;
                $d_map['ploss'] = $yingli;
                $d_map['oid'] = $v['oid'];
                db('order')->update($d_map);
                die('3');
                //买跌
            }elseif($v['ostyle']==1){// && $nowtime >= $v['selltime']

                if($order_cha < 0){  //盈利
                    //$yingli = (($v['fee']*($__beishu)-$v['fee'])/$v['fee'])*$v['fee'];
                    $yingli = ($v['fee']*$__beishu)-$v['fee'];
                    $d_map['is_win'] = 1;


                    //平仓增加用户金额
                    $u_add = $yingli + $fee-$v['sx_fee'];
                    $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                    //写入日志
                    $this->set_order_log($v,$u_add);

                    /*
                    if($v['endloss'] == 60 ){
                        //         总费用 *
                        $yingli = $v['fee']*($v['endloss']/100);
                        $d_map['is_win'] = 1;

                        //平仓增加用户金额
                           $u_add = $yingli + $fee -$v['sx_fee'];
                           $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                           //写入日志
                           $this->set_order_log($v,$u_add);

                   }else{

                           //$yingli  = $order_cha;
                        $yingli = -1*$v['fee'];
                        $d_map['is_win'] = 2;
                   }

                   if($v['endloss'] == 200 ){
                           if($order_cha <-1 && $order_cha >-2)
                           {
                               $yingli = $v['fee']*($v['endloss']/100);
                            $d_map['is_win'] = 1;

                            //平仓增加用户金额
                               $u_add = $yingli + $fee -$v['sx_fee'];
                               $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                               //写入日志
                               $this->set_order_log($v,$u_add);

                           }else
                           {
                               //$yingli  = $order_cha;
                            $yingli = -1*$v['fee'];
                            $d_map['is_win'] = 2;
                           }
                   }

                   if ($v['endloss'] ==500) {

                           if($order_cha <=-2){
                               $yingli = $v['fee']*($v['endloss']/100);
                            $d_map['is_win'] = 1;

                            //平仓增加用户金额
                               $u_add = $yingli + $fee -$v['sx_fee'];
                               $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                               //写入日志
                               $this->set_order_log($v,$u_add);
                           }else{

                           //$yingli  = $order_cha;
                        $yingli = -1*$v['fee'];
                        $d_map['is_win'] = 2;
                       }

                   }
                    // $yingli = $v['fee']*($v['endloss']/100);
                    // $d_map['is_win'] = 1;

                    // //平仓增加用户金额
                     // $u_add = $yingli + $fee;
                     // $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);

                     ////写入日志
                     // $this->set_order_log($v,$u_add);
                    */

                }elseif($order_cha >= 0){	//亏损
                    $yingli = -1*$v['fee'];
                    $d_map['is_win'] = 2;

                }else{		//无效
                    $yingli = 0;
                    $d_map['is_win'] = 3;
                    //平仓增加用户金额
                    $u_add = $fee;
                    $db_userinfo->where('uid',$v['uid'])->setInc('usermoney',$u_add);
                    //写入日志
                    $this->set_order_log($v,$u_add);
                    //----- 处理无效盾部分
                    $yingbi_list = Db::name('yingdun_log')->where('orderno',$v['orderno'])->select();
                    if($yingbi_list){
                        foreach ($yingbi_list as $yb_key=>$yb_val){
                            Db::name('userinfo')->where('uid', $yb_val['uid'])->setDec('moneybi', $yb_val['account']);
                            set_yingdun_log($yb_val['uid'], 2, -$yb_val['account'], $yb_val['title'], $yb_val['content'].'无效处理',$yb_val['oid'],$yb_val['summoneybi'], $yb_val['zhanbi'], $v['orderno'], $yb_val['order_price']);
                        }
                    }


                    die(2);
                }

                //平仓处理订单
                $d_map['ostaus'] = 1;
                $d_map['sellprice'] = $sellprice; //平仓价
                $d_map['ploss'] = $yingli; //盈亏
                $d_map['oid'] = $v['oid'];
                $db_order->update($d_map);
                //dump($d_map);
                die('3');

            }
            ///-----------------------------------------------------------------


            // if(!$oid || !$price || !$order_rand ){
            // 	die('0');
            // }

            // $order = db('order')->where('oid',$oid)->find();


            // //没有此订单
            // if(!$order ){
            // 	die('0');
            // }

            // if($order_rand != cache('goorder_'.$order['oid'])){
            // 	die('0');
            // }


            // //没有平仓
            // if(isset($order['ostyle']) && $order['ostaus'] == 0){  //ostyle 0涨 1跌     ostaus 0 交易 1 平仓
            // 	die('2');
            // }

            // //已平仓 但是价格相同
            // if(isset($order['sellprice']) && $order['sellprice'] == $price){
            // 	cache('goorder_'.$order['oid'],null);
            // 	die('1');
            // }

            // //已平仓 但是无效交易
            // if(isset($order['is_win']) && $order['is_win'] == 3){
            // 	cache('goorder_'.$order['oid'],null);
            // 	die('1');
            // }

            // //该订单指定赢亏
            // if(isset($order['kong_type']) && $order['kong_type'] != 0){
            // 	cache('goorder_'.$order['oid'],null);
            // 	die('1');
            // }

            // //该用户指定赢亏
            // $risk = db('risk')->find();
            // $to_win = explode('|',$risk['to_win']);
            // $to_loss = explode('|',$risk['to_loss']);
            // if(in_array($order['uid'],$to_win) || in_array($order['uid'],$to_loss)){
            // 	cache('goorder_'.$order['oid'],null);
            // 	die('1');
            // }

            //已平仓 但是价格不相同 平仓价 &&当前价格
            // if(isset($order['sellprice']) && $order['sellprice'] != $price){
            // 	//资金变动日志
            // 	$p_map['title'] = '结单';
            // 	$p_map['oid'] = $order['oid'];

            // 	$price_log = db('price_log')->where($p_map)->find();

            // 	$_data['oid'] = $oid;
            // 	//平仓价
            // 	$_data['sellprice'] = $price;

            // 	//买跌 赢利 ||  买涨 赢利  $order['ostyle'] 1涨 0跌      $order['buyprice']入仓价
            // 	// $order['is_win'] 是否盈利  1盈利  2亏损  3无效
            // 	// ostyle  0涨 1跌            buyprice入仓价   < 当前价格
            // 	if($order['ostyle'] == 1 && $order['buyprice'] < $price && $order['is_win'] == 1 ||
            // 		$order['ostyle'] == 0 && $order['buyprice'] > $price && $order['is_win'] == 1){
            // 		//is_win 是否盈利 1盈利 2亏损 3无效

            // 		$_data['is_win'] = 2; //亏损
            // 		$_data['ploss'] = $order['fee']*(-1); //减去用户本金

            // 		$u_add = 0;

            // 		db('userinfo')->where('uid',$price_log['uid'])->setDec('usermoney',$price_log['account']);

            // 		$price_log['account'] = round($price_log['account']/(1-$order['endloss']/100),2);
            //              	$price_log['type'] = 1;
            //              	$price_log['oid'] = $p_map['oid'];
            //              	$ids = db('price_log')->update($price_log);


            //              	db('order_log')->where('oid',$order['oid'])->delete();
            //              	//写入日志
            //           	$api = controller('api');
            //           	$api->set_order_log($order,$u_add);
            // 	}

            // 	//买跌 亏损 ||  买涨 亏损
            // 	// $order['is_win'] 是否盈利  1盈利  2亏损  3无效
            // 	//     ostyle  0涨 1跌               入仓价  > 平仓价
            // 	if($order['ostyle'] == 1 && $order['buyprice'] > $price && $order['is_win'] == 2 ||
            // 		$order['ostyle'] == 0 && $order['buyprice'] < $price && $order['is_win'] == 2){

            // 		$_data['is_win'] = 1;//盈利

            // 		//$xianjia = get_price();
            // 		//$xianopen = get_open();
            // 		//-----------------------------------------------------------------------------
            // 		///$chajia = round($xianjia-$xianopen,2);  // 差价 = 现价-开盘价  四舍五入 取两位小数点
            // 		//$baifen = round($chajia/$xianjia*100,2);
            //         	//$baifenbi = round($chajia/$xianjia*100,2)."％";//  涨幅 = 差价 / 现价*100    四舍五入
            //         	//-----------------------------------------------------------------------------

            // 		//用户盈利       总费用建仓用的金额 * (盈亏比例/100)
            // 		$yingli = $order['fee']*($order['endloss']/100);
            // 		$_data['ploss'] = $yingli;//最终的用户盈利

            // 		//平仓增加用户金额
            //              	$u_add = $yingli + $order['fee'];
            //              	db('userinfo')->where('uid',$order['uid'])->setInc('usermoney',$u_add);
            //              			////变动金额                     变动金额 *  (盈亏比例/100)
            //              	$price_log['account'] = round($price_log['account']*($order['endloss']/100),2);
            //              	$price_log['type'] = 2;
            //               //$price_log['oid'] = $p_map['oid'];
            //              $ids = db('price_log')->where('oid', $p_map['oid'])->update($price_log);
            //              //$ids = db('price_log')->update($price_log);

            //              	db('order_log')->where('oid',$order['oid'])->delete();
            //              	//写入日志
            //           	$api = controller('api');
            //           	$api->set_order_log($order,$u_add);

            // 	}
            // 	//更新用户订单建仓的价格
            // 	db('order')->update($_data);
            // 	//dump($_data);
            // 	cache('goorder_'.$order['oid'],null);
            // 	die('3');

            // }

        }
    }



    /**
     * 写入平仓日志
     * @author lukui  2017-07-01
     * @param  [type] $v        [description]
     * @param  [type] $addprice [description]
     */
    public function set_order_log($v,$addprice)
    {
        $o_log['uid'] = $v['uid'];
        $o_log['oid'] = $v['oid'];
        $o_log['addprice'] = $addprice;
        $o_log['addpoint'] = 0;
        $o_log['time'] = time();
        $o_log['user_money'] = db('userinfo')->where('uid',$v['uid'])->value('usermoney');
        db('order_log')->insert($o_log);

        //资金日志
        set_price_log($v['uid'],1,$addprice,'结单','订单到期获利结算',$v['oid'],$o_log['user_money']);
    }


    /**
     * 订单类型
     * @param  [type] $orders [description]
     * @return [type]         [description]
     */

    public function order_type22($orders,$pro,$risk,$data_info)
    {

        $pid = $pro['pid'];
        $thispro = array();		//买此产品的用户
        //此产品购买人数
        $price_num = 0;
        //买涨金额，计算过盈亏比例以后的
        $up_price = 0;
        //买跌金额，计算过盈亏比例以后的
        $down_price = 0;
        //买入最低价
        $min_buyprice = 0;
        //买入最高价
        $max_buyprice = 0;
        //下单最大金额
        $max_fee = 0;
        //指定客户亏损
        $to_win = explode('|',$risk['to_win']);
        $is_to_win = array();
        //指定客户亏损
        $to_loss = explode('|',$risk['to_loss']);
        $is_to_loss = array();

        $i = 0;
        foreach ($orders as $k => $v) {

            if($v['pid'] == $pid ){
                //没炒过最小风控值直接退出price
                if ($v['fee'] < $risk['min_price']) {
                    //return $pro['Price'];
                }
                $i++;

                //单控 赢利
                if($v['kong_type'] == 1){
                    $dankong_ying = $v;
                }

                //单控 亏损
                if($v['kong_type'] == 2){
                    $dankong_kui = $v;
                }

                //是否存在指定盈利
                if(in_array($v['uid'], $to_win)){
                    $is_to_win = $v;

                }
                //是否存在指定亏损
                if(in_array($v['uid'], $to_loss)){
                    $is_to_loss = $v;

                }

                //总下单人数
                $price_num++;
                //买涨买跌累加
                if($v['ostyle'] == 0){
                    $up_price += $v['fee']*$v['endloss']/100;
                }else{
                    $down_price += $v['fee']*$v['endloss']/100;
                }
                //统计最大买入价与最大下单价
                // if($i == 1){
                // 	$min_buyprice = $v['buyprice'];
                // 	$max_buyprice = $v['buyprice'];
                // 	$max_fee = $v['fee'];
                // }else{
                // 	if($min_buyprice > $v['buyprice']){
                // 		$min_buyprice = $v['buyprice'];

                // 	}
                // 	if($max_buyprice < $v['buyprice']){
                // 		$max_buyprice = $v['buyprice'];
                // 	}
                // 	if($max_fee < $v['fee']){
                // 		$max_fee = $v['fee'];
                // 	}
                // }
            }

        }

        // dump('$pid:'.$pid);
        // dump('$price_num:'.$price_num);
        // dump('$up_price:'.$up_price);
        // dump('$down_price:'.$down_price);
        // dump('$min_buyprice:'.$min_buyprice);
        // dump('$max_buyprice:'.$max_buyprice);
        // dump('$max_fee:'.$max_fee);

        //根据现在的价格算出风控点
        // $FloatLength = getFloatLength((float)$pro['Price']);
        // $jishu_rand = pow(10,$FloatLength);
        // $beishu_rand = rand(1,10);

        // $data_rands = $data_info->where('pid',$pro['pid'])->value('rands');
        // $data_randsLength = getFloatLength($data_rands);
        // if($data_randsLength > 0){
        // 	$_j_rand = pow(10,$data_randsLength)*$data_rands;
        // 	$_s_rand = rand(1,$_j_rand)/pow(10,$data_randsLength);
        // }else{
        // 	$_s_rand = 0;
        // }


        //$do_rand = $_s_rand;

        //是否存在指定盈利
        $is_do_price = 0; 	//是否已经操作了价格

        //先考虑单控
        // if(!empty($dankong_ying) && $is_do_price == 0){ 		//单控 1赢利
        // 	if($dankong_ying['ostyle'] == 0 && $pro['Price'] < $dankong_ying['buyprice']){
        // 		$pro['Price'] = $v['buyprice'] + $do_rand;
        // 	}elseif($dankong_ying['ostyle'] == 1 && $pro['Price'] > $dankong_ying['buyprice']){
        // 		$pro['Price'] = $v['buyprice'] - $do_rand;
        // 	}
        // 	$is_do_price = 1;
        // }

        // if(!empty($dankong_kui) && $is_do_price == 0){ 		//单控 2亏损
        // 	if($dankong_kui['ostyle'] == 0  && $pro['Price'] > $dankong_kui['buyprice']){
        // 		$pro['Price'] = $v['buyprice'] - $do_rand;
        // 	}elseif($dankong_kui['ostyle'] == 1 && $pro['Price'] < $dankong_kui['buyprice']){
        // 		$pro['Price'] = $v['buyprice'] + $do_rand;
        // 	}
        // 	$is_do_price = 1;
        // 	$is_do_price = 1;
        // }

        //指定客户赢利
        if(!empty($is_to_win) && $is_do_price == 0){

            if($is_to_win['ostyle'] == 0 && $pro['Price'] < $is_to_win['buyprice']){
                $pro['Price'] = $v['buyprice'] + $do_rand;
            }elseif($is_to_win['ostyle'] == 1 && $pro['Price'] > $is_to_win['buyprice']){
                $pro['Price'] = $v['buyprice'] - $do_rand;
            }
            $is_do_price = 1;
            //echo 1;

        }
        //是否存在指定亏损
        if(!empty($is_to_loss) && $is_do_price == 0){


            if($is_to_loss['ostyle'] == 0  && $pro['Price'] > $is_to_loss['buyprice']){
                $pro['Price'] = $v['buyprice'] - $do_rand;
            }elseif($is_to_loss['ostyle'] == 1 && $pro['Price'] < $is_to_loss['buyprice']){
                $pro['Price'] = $v['buyprice'] + $do_rand;
            }
            $is_do_price = 1;
            //echo 2;
        }
        //没有任何下单记录
        if($up_price == 0 && $down_price == 0 && $is_do_price == 0){
            $is_do_price = 1;
            //return $pro['Price'];
        }

        //只有一个人下单，或者所有人下单买的方向相同
        // if(( ($up_price == 0 && $down_price != 0) || ($up_price != 0 && $down_price == 0) )  && $is_do_price == 0 ){

        // 	//风控参数
        // 	$chance = $risk["chance"];
        // 	$chance_1 = explode('|',$chance);
        // 	$chance_1 = array_filter($chance_1);
        // 	//循环风控参数
        // 	if(count($chance_1) >= 1){
        // 		foreach ($chance_1 as $key => $value) {
        // 			//切割风控参数
        // 			$arr_1 = explode(":",$value);
        // 			$arr_2 = explode("-",$arr_1[0]);
        // 			//比较最大买入价格
        // 			if($max_fee >= $arr_2[0] && $max_fee < $arr_2[1]){
        // 				//得出风控百分比
        // 				$chance_num = $arr_1[1];
        // 				$_rand = rand(1,100);

        // 			}

        // 		}
        // 	}

        // 	//买涨
        // 	if(isset($_rand) && $up_price != 0){

        // 		if($_rand > $chance_num){	//客损
        // 			$pro['Price'] = $max_buyprice-$do_rand;
        // 			$is_do_price = 1;
        // 			//echo 3;
        // 		}else{		//客赢
        // 			$pro['Price'] = $min_buyprice+$do_rand;
        // 			$is_do_price = 1;
        // 			//echo 4;
        // 		}

        // 	}
        // 	if(isset($_rand) && $down_price != 0){

        // 		if($_rand > $chance_num){	//客损
        // 			$pro['Price'] = $min_buyprice+$do_rand;
        // 			$is_do_price = 1;
        // 			//echo 5;
        // 		}else{		//客赢
        // 			$pro['Price'] = $max_buyprice-$do_rand;
        // 			$is_do_price = 1;
        // 			//echo 6;
        // 		}

        // 	}



        // }

        //多个人下单，并且所有人下单买的方向不相同
        // if($up_price != 0 && $down_price != 0  && $is_do_price == 0){
        // 	//买涨大于买跌的
        // 	if ($up_price > $down_price) {
        // 		$pro['Price'] = $min_buyprice-$do_rand;
        // 		$is_do_price = 1;
        // 		//echo 7;

        // 	}
        // 	//买涨小于买跌的
        // 	if ($up_price < $down_price) {
        // 		$pro['Price'] = $max_buyprice+$do_rand;
        // 		$is_do_price = 1;
        // 		//echo 8;
        // 	}


        // }
        db('productdata')->where('pid',$pro['pid'])->update($pro);

        if($pro['Price'] < $pro['Low']){
            $pro['Price'] = $pro['Low'];
        }
        if($pro['Price'] > $pro['High']){
            $pro['Price'] = $pro['High'];
        }
        return $pro['Price'];
    }

    public function ajaxtouzhu_info(){
        //订单
        $uid = $this->uid;;
        $where['uid'] = $uid;
        // $where['ostaus'] = 1;
        $order_list = Db::name('order')->where($where)->order('oid desc')->paginate(5);
        $str ='';
        if($order_list){


            foreach ($order_list as $key=>$val){
                $str .='<tr>';
                // $str .='<td>账户名</td>';
                $ostyle ='跌';
                if($val['ostyle']==0){
                    $ostyle ='涨';
                }
                $str .='<td>'.date('Y-m-d H:i:s',$val['buytime']).'</td>';
                $str .='<td>'.$ostyle.'</td>';
                $str .='<td>'.$val['fee'].'</td>';
                $str .='<td>'.$val['buyprice'].' </td>';
                $str .='<td>'.$val['sellprice'].'</td>';
                $str .='<td>'.$val['ploss'].'</td>';
                $str .='<td>'.$val['sx_fee'].'</td>';
                $str .='</tr>';
            }


        }else{
            $str ='<tr><td colspan="8">暂时没有数据</td></tr>';
        }
        $data['info'] =$str;
        return json($data);


    }



}
